<template>
	<view class="content">
		<view class="input-group">
			<view class="input-row border">
				<text class="title">账号：</text>
				<m-input class="m-input" type="text" clearable focus v-model="account" placeholder="请输入账号"></m-input>
			</view>
			<view class="input-row">
				<text class="title">密码：</text>
				<m-input type="password" displayable v-model="password" placeholder="请输入密码"></m-input>
			</view>
		</view>
		<view class="btn-row">
			<button type="primary" class="primary" @tap="bindLogin">登录</button>
		</view>
		<view class="action-row">
			<navigator url="../pwd/pwd">忘记密码</navigator>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import mInput from '../../components/m-input.vue'

	export default {
		components: {
			mInput
		},
		data() {
			return {
				account: '',
				password: '',
				positionTop: 0
			}
		},
		computed: mapState(['forcedLogin']),
		methods: {
			...mapMutations(['login','setName','setDeptName','setPwd']),
			bindLogin() {
				/**
				 * 客户端对账号信息进行一些必要的校验。
				 */
				if (this.account.length < 5) {
					uni.showToast({
						icon: 'none',
						title: '账号最短为 5 个字符'
					});
					return;
				}
				if (this.password.length < 6) {
					uni.showToast({
						icon: 'none',
						title: '密码最短为 6 个字符'
					});
					return;
				}
				// 向服务器发登录请求
				var serverUrl=this.serverUrl;
				uni.request({
					url: serverUrl + 'app/login',
					method: 'GET',
					data: {
						username: this.account,
						pwd:this.password
					},
					success: res => {
						var result=res.data;
						if(result.ok){
							// 向全局变量里存值
							this.setName(result.name);
							this.setDeptName(result.deptName);
							this.setPwd(this.password);
							// 跳转主页面
							this.toMain(this.account);
						}else{
							uni.showToast({
								title: result.msg,
								icon: 'none'
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '网络连接失败...',
							icon: 'none'
						});
					}
				});
			},
			
			/* 跳转主页面 */
			toMain(userName) {
				this.login(userName);
				uni.reLaunch({
					url: '../main/main',
				});
				
/* 				if (this.forcedLogin) {
					uni.reLaunch({
						url: '../main/main',
					});
				} else {
					// uni.switchTab({
					// 	url:'../main/main'
					// });
					uni.navigateBack();
				}
 */
			}
		},
		onReady() {

		}
	}
</script>

<style>
	.action-row {
		display: flex;
		flex-direction: row;
		justify-content: center;  /* 主轴对齐方式 */
	}

	.action-row navigator {
		color: #007aff;
		padding: 0 10px;
	}

	.oauth-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
	}

	.oauth-image {
		position: relative;
		width: 50px;
		height: 50px;
		border: 1px solid #dddddd;
		border-radius: 50px;
		margin: 0 20px;
		background-color: #ffffff;
	}

	.oauth-image image {
		width: 30px;
		height: 30px;
		margin: 10px;
	}

	.oauth-image button {
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		opacity: 0;
	}
</style>
